import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
const actions = {
    incrementIfOdd(ministate,value){
        if(state.sum % 2){
        ministate.commit('INCREMENT_IF_ODD',value)
        }
    },
    incrementWait(ministate,value){
        setTimeout(()=>{
        ministate.commit('INCREMENT_WAIT',value)
    },500)
    },
    incrementServer(ministate){
        axios.get('https://v1.hitokoto.cn/?c=b&c=a').then(
            response => {
                ministate.commit('INCREMENT_SERVER',response.data.hitokoto)
            },
            error => {
                alert('请求发送失败',error)
            }
        )
    }
}
const mutations = {
    INCREMENT(state,value){
        state.sum += value
    },
    DECREMENT(state,value){
        state.sum -= value
    },
    INCREMENT_IF_ODD(state,value){
        state.sum += value
    },
    INCREMENT_WAIT(state,value){
        state.sum += value
    },
    INCREMENT_SERVER(state,value){
        state.sum += value.length
    }

}
const state = {
    sum:0
}
const getters = {
        bigSum(){
            return state.sum *10
        }
}
export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters

})